package com.wx.service.impl;

import com.wx.dto.BookPageQueryDTO;
import com.wx.dto.BookTypeDTO;
import com.wx.entity.Book;
import com.wx.entity.BookType;
import com.wx.exception.BaseException;
import com.wx.mapper.BookMapper;
import com.wx.result.PageResult;
import com.wx.service.BookService;
import com.wx.vo.BookTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
//todo:用户新增书籍页面优化

    @Override
    public void addtype(BookTypeDTO bookTypeDTO) {
        BookType typeByName = bookMapper.getTypeByName(bookTypeDTO.getName());
        //如果存在，代表分类名已有
        if(typeByName != null){
            throw new BaseException("分类名已存在");
        }
        BookType bookType = BookType.builder()
                .name(bookTypeDTO.getName())
                .sort(bookTypeDTO.getSort())
                .createTime(LocalDateTime.now())
                .createUser(bookTypeDTO.getUsername())
                .updateTime(LocalDateTime.now())
                .updateUser(bookTypeDTO.getUsername())
                .build();
        bookMapper.addtype(bookType);
    }

    @Override
    public List<BookTypeVO> getType(BookTypeDTO bookTypeDTO) {
        return bookMapper.getTypeVO(bookTypeDTO);
    }

    @Override
    public BookType getTypeByid(Integer id) {

        return bookMapper.getTypeById(id);
    }

    @Override
    public void updatetype(BookTypeDTO bookTypeDTO) {
        BookType bookType = BookType.builder()
                .name(bookTypeDTO.getName())
                .sort(bookTypeDTO.getSort())
                .updateUser(bookTypeDTO.getUsername())
                .updateTime(LocalDateTime.now())
                .id(bookTypeDTO.getBookId())
                .build();
        bookMapper.updatetype(bookType);
    }

    @Override
    public void deletetype(Integer id) {
        //判断该分类下是否有书
        Integer integer = bookMapper.queryBookByBookType(id);
        if(integer > 0){
            throw new BaseException("请先移除该分类下的图书");
        }

        bookMapper.deletetype(id);
    }

    @Override
    public List<BookType> getTypeName() {
        List<BookType> typelist = bookMapper.getType();
        return typelist;
    }

    @Override
    public void addbook(Book book) {
        //查询图书编号是否存在
        Book book1 = bookMapper.queryByBookNo(book.getBookNo());
        if(book1 != null){
            throw new BaseException("图书编号重复");
        }

        book.setAddTime(LocalDateTime.now());
        book.setUpdateTime(LocalDateTime.now());
        bookMapper.addbook(book);
    }

    @Override
    public PageResult queryBook(BookPageQueryDTO bookPageQueryDTO) {
        //分页查询插件失效
       /* PageHelper.startPage(bookPageQueryDTO.getPage(), bookPageQueryDTO.getPageSize());
        Page<BookQueryVO> pageResult = bookMapper.pageQuery(bookPageQueryDTO);

        return new PageResult(pageResult.getTotal(), pageResult.getResult());*/

        //起始索引=（页码-1）*每页记录数      limit 起始索引，页面记录数
        PageResult pageResult = new PageResult();
        bookPageQueryDTO.setPage((bookPageQueryDTO.getPage()-1)* bookPageQueryDTO.getPageSize());

        pageResult.setTotal(bookMapper.queryBookCount(bookPageQueryDTO));
        pageResult.setRecords(bookMapper.pageQuery(bookPageQueryDTO));
        return pageResult;
    }

    @Override
    public Book getBookById(Integer id) {
        Book book = bookMapper.queryBookById(id);
        if(book == null){
            throw new BaseException("id不存在");
        }

        return book;
    }

    @Override
    public void delebook(Book book) {
        /*查询该图书是否有人借阅*/
        Integer num = bookMapper.examinebook(book);
        if(num > 0){
            throw new BaseException("该图书有人借阅");
        }

        /*删除图书*/
        bookMapper.deleBook(book);
    }

    //更新图书信息
    @Override
    public void updatebook(Book book) {
        book.setUpdateTime(LocalDateTime.now());
        bookMapper.updateBookByBookNo(book);
    }

}
